package com.axios.controller.servlets;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author lwj
 * @date 2024-11-19
 * @vesion 1.0.0
 */
@WebServlet("/api/login.do")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应头允许跨域
        response.setHeader("Access-Control-Allow-Origin", "*");

        // 响应头
        response.setHeader("Access-Control-Allow-Headers", "*");

        // 获取参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        // 创建输出流对象
        PrintWriter out = response.getWriter();

        // 判断是否符合
        if("admin".equals(username) && "123".equals(password)){
            System.out.println(1);
            out.write("登录成功！");
        }else{
            System.out.println(2);
            out.write("用户名或密码错误！");
        }
        // 刷新输出流
        out.flush();
        // 关闭输出流
        out.close();
    }
}
